# -*- coding: utf-8 -*-
"""
Created on Sat Oct  9 15:32:29 2021
本代码旨在研究selenium，无任何其他用途，请勿用于其他途径。
"""

import time
from selenium import webdriver
import random
from multiprocessing import Pool
import os
from aip import AipOcr
from PIL import Image
#!!!!需要在57行对应的位置创建临时文件夹!!!!
APP_ID = '20511307'#填入你的AppID
API_KEY = 'YCEQIEjinAaEWvRtnjZFT3LG'#填入你的API Key
SECRET_KEY = 'hfmuRcfWwd6PwjTqgn9B0o7iV03XgrQX'#填入你的Secret Key
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()
people = []
classnums = ['']#填入你的学号，多个用","分割!!!
passages = ['']#填入你的密码，多个用","分割，且与学号一一对应!!!
for i in range(0,len(classnums),1):
    classnum = classnums[i]
    passage = passages[i]
    get_dict = {
        'classnum': classnum, 
        'passage': passage
    }
    people.append(get_dict)

def stop(number):
    time.sleep(number)

def rands():
    b = '{:.1f}'.format(random.uniform(35.4, 36.6))
    return b
def write_tem(get_dict):
    options = webdriver.ChromeOptions()
    options.add_argument('--ignore-certificate-errors')
    print(get_dict['classnum']+"正在自动填写体温")
    driver = webdriver.Chrome(options=options)
    login_url="https://web-vpn.sues.edu.cn"
    driver.get(login_url)
    stop(4)
    driver.find_element_by_id("username").click()
    driver.find_element_by_id("username").send_keys(get_dict['classnum'])

    stop(1)
    driver.find_element_by_id("password").click()
    driver.find_element_by_id("password").send_keys(get_dict['passage'])

    stop(1)
    yzm_img=driver.find_element_by_xpath("//*[@id='fm1']/div[4]/img")
    driver.save_screenshot("C:\\temp\\05.png")  # 对整个浏览器页面进行截图,记得创建临时文件夹
    left = yzm_img.location['x']
    top = yzm_img.location['y']
    right = yzm_img.location['x'] + yzm_img.size['width']
    bottom = yzm_img.location['y'] + yzm_img.size['height']
    im = Image.open('C:\\temp\\05.png')
    im = im.crop((left, top, right, bottom))  # 对浏览器截图进行裁剪
    im.save('C:\\temp\\06.png')
    image = get_file_content('C:\\temp\\06.png')#对百度OCR请求
    options = {}
    options['language_type'] = 'CHN_ENG'
    options['detect_direction'] = 'true'
    options['detect_language'] = 'true'
    result = {}
    result=client.basicGeneral(image, options)
    result_list=result["words_result"]
    result_string=""#取得结果字符串
    for item in result_list:
     for str in item:
      result_string=result_string+item[str]
    print(result_string)
    driver.find_element_by_xpath("//*[@id='authcode']").send_keys(result_string)#填入验证码
    stop(2)
    driver.find_element_by_class_name("login_btn").click()

    stop(2)
    try:
         driver.find_element_by_xpath("//*[@id='group-4']/div[2]/div/div[2]").click()
         stop(5)
         driver.refresh()
    except:
        driver.quit()
        write_tem(get_dict)
    else:
        stop(1)
        windows = driver.window_handles
        driver.switch_to.window(windows[1])

        stop(2)
        driver.find_element_by_xpath("//*[@id='form']/div[18]/div[1]/div/div[2]/div/div/input").click()

        stop(2)
        driver.find_element_by_xpath("//*[@id='form']/div[18]/div[1]/div/div[2]/div/div/input").clear()

        stop(2)
        driver.find_element_by_xpath("//*[@id='form']/div[18]/div[1]/div/div[2]/div/div/input").send_keys(rands())

        stop(2)
        driver.find_element_by_xpath("//*[@id='post']").click()

        stop(2)
        driver.quit()
    

    print(get_dict['classnum']+"体温填写完毕")


if __name__ == '__main__':
    pool = Pool(processes=3)
    pool.map(write_tem, people)
    pool.close()
    pool.join()

